package com.java.base.exercise.designmode.responsibilitychain;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author RenPu
 * @Date 2021/6/27 9:28
 * @Version 1.0
 * @Description: 责设计模式--责任链模式----request请求+response请求的调用v2.0 集合的形式实现：责任链
 **/
public class ResponsibilityChain1 {

    public static void main(String[] args) {

        //模仿请求进来访问输入的内容，有一些敏感词进行过滤
        Request message = new Request();
        message.msg = "大家好:),<script>,欢迎访问 www.baidu.com, 大家都是007";

        Response response = new Response();
        response.msg = "response--------";


        /**---------------1.0过滤----------------------*/
//        String msg = message.getMsg();
//        msg=msg.replace('<','[');
//        msg=msg.replace('>',']');
//        message.setMsg(msg);
//        msg=msg.replaceAll("007","955");
//        message.setMsg(msg);

//        SymbolFilter symbolFilter = new SymbolFilter();
//        symbolFilter.doFilter(message);
//        NumberFilter numberFilter = new NumberFilter();
//        numberFilter.doFilter(message);


        /**---------------2.0过滤----------------------*/
//        List<FilterDefinnte> definnteList = new ArrayList<>();
//        definnteList.add(new SymbolFilter());
//        definnteList.add(new NumberFilter());
//
//        for (FilterDefinnte definnte: definnteList) {
//            definnte.doFilter(message);
//        }
//


        /**---------------3.0过滤----------------------*/
        ChainFilters chainFilter = new ChainFilters();
        chainFilter.add(new SymbolFiltero()).add(new NumberFiltero());
        chainFilter.doFilter(message, response, chainFilter);
        System.out.println("Request Result == RR =="+message.msg);
        System.out.println("Response Result == RS =="+response.msg);


    }


}

class Request {
    String name;
    String msg;


    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "Message{" +
                " msg='" + msg + '\'' +
                '}';
    }

}

class Response {

    String name;
    String msg;


    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "Message{" +
                " msg='" + msg + '\'' +
                '}';
    }

}


//创建接口，使用接口实现多态的展示
interface FilterDefinde {
    boolean doFilter(Request message, Response response, ChainFilters chainFilter);
}


class ChainFilters implements FilterDefinde {
    volatile int  index = 0;
    List<FilterDefinde> definnteList = new ArrayList<>();
    /**
     * 添加方法
     */
    public ChainFilters add(FilterDefinde filterDefinnte) {
        definnteList.add(filterDefinnte);
        return this;
    }


    /**
     * 执行方法
     */
    @Override
    public boolean doFilter(Request message, Response response, ChainFilters chainFilter) {
        if (index == definnteList.size()) return false;
        FilterDefinde filterDefinde = definnteList.get(index);
        index++;
        return filterDefinde.doFilter(message, response, chainFilter);
    }
}

//过滤符号的实现
class SymbolFiltero implements FilterDefinde {

    @Override
    public boolean doFilter(Request message, Response response, ChainFilters chainFilter) {
        message.msg = message.msg.replaceAll("<", "[").replaceAll("<", "[") + "Symbol Filter-----------";
        chainFilter.doFilter(message, response, chainFilter);
        response.msg += "-------Symbol Respinse+++++++++";
        return true;
    }
}


//过滤数字
class NumberFiltero implements FilterDefinde {
    @Override
    public boolean doFilter(Request message, Response response, ChainFilters chainFilter) {
        message.msg = message.msg.replaceAll("<", "[").replaceAll("<", "[") + "Number Filter-----------";
        chainFilter.doFilter(message, response, chainFilter);
        response.msg += "-------Number Respinse+++++++++";
        return true;
    }
}